package com.haircut.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haircut.base.Result;
import com.haircut.enums.UserType;
import com.haircut.exception.BusinessException;
import com.haircut.mapper.HaircutUserOrder;
import com.haircut.mapper.HaircutUserRecharge;
import com.haircut.model.dto.*;
import com.haircut.model.page.PageParam;
import com.haircut.model.req.*;
import com.haircut.security.util.SecurityUtils;
import com.haircut.service.HaircutConsumptionItemService;
import com.haircut.service.HaircutUserOrderService;
import com.haircut.service.HaircutUserRechargeService;
import com.haircut.service.WechatUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/v1/admin")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "后台")
public class AdmInController {

    private final HaircutUserRechargeService rechargeService;
    private final HaircutConsumptionItemService consumptionItemService;
    private final HaircutUserOrderService orderService;
    private final WechatUserService wechatUserService;

    @Operation(summary = "所有用户充值记录")
    @PostMapping("/rechargeList")
    public Result<Page<UserRechargeDTO>> rechargeList(@RequestBody RechargeReq req) {
        Page<UserRechargeDTO> list = rechargeService.getRechargeAll(req);
        return Result.success(list);
    }

    @Operation(summary = "充值")
    @PostMapping("/recharge")
    public Result recharge(@RequestBody @Valid AdminRechargeReq rechargeReq) {
        if(checkUser(SecurityUtils.getRoles())){
            rechargeService.adminRecharge(rechargeReq);
        }
        return Result.success();
    }

//    @Operation(summary = "消费记录")
//    @GetMapping("/orderList")
//    public Result<List<UserOrderDTO>> orderList(@RequestParam(required = false) String phone) {
//        List<HaircutUserOrder> list = orderService.getOrders(null,phone);
//        List<UserOrderDTO> userOrderDTOS = BeanUtil.copyToList(list, UserOrderDTO.class);
//        return Result.success(userOrderDTOS);
//    }

    @PostMapping("/orderList")
    public Result<UserOrderResp> orderList(@RequestBody @Valid OrderListReq orderListReq) {
        return orderService.getOrders(orderListReq);
    }

    @Operation(summary = "购买")
    @PostMapping("/buy")
    public Result buy(@RequestBody @Valid AdminBuyReq buyReq) {
        if(checkUser(SecurityUtils.getRoles())){
            consumptionItemService.buy(buyReq);
        }
        return Result.success();
    }

    @PostMapping("/userList")
    public Result<UserListDto> userList(@RequestBody PageParam page){
        return wechatUserService.userList(page);
    }

    /**
     * 查询找个用户 通过手机号
     * @param phone
     * @return Result
     */
    @GetMapping("/user/{phone}")
    public Result<UserDTO> singleUser(@PathVariable String phone){
        return wechatUserService.singleUser(phone);
    }

    private boolean checkUser(Set<String> roles) {
        if(roles!=null&& roles.contains(UserType.ADMIN.getCode())){
            return true;
        }
        throw new BusinessException("用户没有权限");
    }

    @PostMapping("/addRemark")
    public Result<String> addRemark(@RequestBody RemarkReq req){
        return wechatUserService.addRemark(req);
    }
}
